package rickyGame.game.scene
{
	import flash.display.DisplayObject;
	import flash.display.DisplayObjectContainer;
	import flash.display.Sprite;
	import flash.events.EventDispatcher;
	import flash.events.MouseEvent;
	import flash.geom.Point;
	
	import ricky.animate.AnimSprite;
	import ricky.animate.Animate;
	import ricky.game.Actor;
	import ricky.game.Actor2D;
	import ricky.game.graphic.Graphic;
	import ricky.loader.LoadingView;
	import ricky.quadtree.Leaf;
	import ricky.quadtree.Quadtree;
	import ricky.scene.MiniMap;
	import ricky.scene.Scene;
	
	import rickyGame.controller.Controller;
	import rickyGame.events.SceneEvent;
	import rickyGame.game.GameState;
	import rickyGame.game.graphic.PlayerGraphic;
	import rickyGame.ui.UIManager;

	/**
	 * 场景管理器
	 * 管理场景里的资源和角色
	 */
	public class SceneManager extends EventDispatcher
	{
		public function SceneManager()
		{
			_objects=new Array();
		}
		
		//********************* 对外方法 *********************
		/**
		 * 加载新场景
		 * @param mapURL:String 对应的地图配置文件
		 */
		public function loadMap(mapURL:String,onproccess:Function=null):void
		{
			if(_currScene)
				_currScene.clear();

			_isLoading=false;
			
			if(!_currScene)
				_currScene=new Scene();
			_currScene.loadMap(mapURL,onSceneDataLoaded,onproccess);
			_currScene.addEventListener(MouseEvent.CLICK,onClick);
			_stage.addChildAt(_currScene,0);

//			this.
//			_loadingView=_loadingScene.loadingView;
//			_stage.addChild(_loadingScene.loadingView);
		}
		
		/**
		 * 清理场景
		 */
		public function clearScene():void
		{
			
		}
		
		/**
		 * 添加对象到场景
		 * @param pu:Pu 游戏对象
		 */
		public function addGameObject(pu:Actor2D):Boolean
		{
			if(_currScene!=null)
			{
				var i:int=_objects.indexOf(pu);
				if(i==-1)
					_objects.push(pu);
//				pu.updatepostion();
				this._objects.push(pu);
				_currScene.characterLayout.addChild(PlayerGraphic(pu.graphic));
				return true;
			}
			return false;
		}
		
		public function orderY():void
		{
			_objects.sortOn("y",Array.NUMERIC);	
			var i:int=0;
			for each(var p:* in _objects)
			{
				this.setGameObjectIndex(p.actor,i);
				i++;
			}
		}
		
		/**
		 * 深度排序
		 * @param pu:Pu 游戏对象
		 * @param index:int 索引
		 */
		private function setGameObjectIndex(pu:Actor,index:int):void
		{
			if(_currScene!=null)
			{
				var sp:Sprite=_currScene.characterLayout;
				if(index>=sp.numChildren)
					index=sp.numChildren-1;
				sp.setChildIndex(PlayerGraphic(pu.graphic),index);

			}
		}
		
		/**
		 * 添加DisplayObject对象到场景
		 * @param dis:DisplayObject flash对象
		 */
		public function addFlashObject(dis:DisplayObject):Boolean
		{
			if(_currScene!=null)
			{
				_currScene.characterLayout.addChild(dis);
				return true;
			}
			return false;
		}
		
		/**
		 * 从场景中删除DisplayObject对象
		 * @param pdis:DisplayObject DisplayObject对象
		 */
		public function removeFlashObject(dis:DisplayObject):Boolean
		{
			if(_currScene!=null)
			{
				if(dis.parent)
					dis.parent.removeChild(dis);
				return true;
			}
			return false;
		}
		
		/**
		 * 从场景中删除对象
		 * @param pu:Pu 游戏对象
		 */
		public function removeGameObject(pu:Actor2D):Boolean
		{
			if(_currScene!=null)
			{
				var i:int=_objects.indexOf(pu);
				if(i>=0)
					_objects.splice(i,1);
				var reobj:DisplayObject;
				if(PlayerGraphic(pu.graphic).parent)
					reobj=PlayerGraphic(pu.graphic).parent.removeChild(PlayerGraphic(pu.graphic));
				return reobj!=null;
			}
			return false;
		}
		
		/**
		 * 杀死
		 */
		public function killGameObject(pu:Actor2D):Boolean
		{
			if(_currScene!=null)
			{
				var i:int=_objects.indexOf(pu);
				if(i>=0)
					_objects.splice(i,1);
				this._currScene.tree.remove(pu,pu.morton);
				var reobj:DisplayObject;
				if(PlayerGraphic(pu.graphic).parent)
					reobj=PlayerGraphic(pu.graphic).parent.removeChild(PlayerGraphic(pu.graphic));
				return reobj!=null;
			}
			return false;
		}
		
		/**
		 * 得到一个点周围的所有玩家</br>
		 * <b>p:Point</b> 中心对象</br>
		 * <b>radius:uint</b>  格子半径</br>
		 * <b>max:uint</b>  最大数量</br>
		 */
		public function getPointAround(x:Number,y:Number,radius:uint,max:uint=0):Array
		{
			var cx:uint=x/Common.tileWidth;
			var cy:uint=y/Common.tileHeight;
			
			return getGridAround(cx,cy,radius,max);
		}
		
		/**
		 * 得到一个格子周围的所有对象</br>
		 * 参数：</br>
		 * 
		 */
		public function getGridAround(gridx:int,gridy:int,radius:int,max:uint=0):Array
		{
			
			var x1:int=gridx-radius;
			var x2:uint=gridx+radius;
			
			var y1:int=gridy-radius;
			var y2:int=gridy+radius;
			
			if(x1<=0)
				x1=0;
			if(y1<=0)
				y1=0;
			var re:Array=this._currScene.tree.getArea(x1,y1,x2,y2);
			
			if(re.length>=max && max!=0)
				re.length=max;
			
			return re;		
		}
		
		public function remove(name:String):void
		{
			var a:Actor2D=this.getActorByName(name);
			if(a)
				this.removeGameObject(a);
		}
		
		/**
		 * 得到指定角色名字的控制器
		 */
		public function getActorByName(name:String):Actor2D
		{
			for each(var a:Actor2D in this._objects)
			{
				if(name==a.name)
				{
					return a;
				}
			}
			
			return null;
		}
//		
//		/**
//		 * 得到一个点周围的所有玩家</br>
//		 * <b>p:Point</b> 中心对象</br>
//		 * <b>radius:uint</b>  格子半径</br>
//		 * <b>max:uint</b>  最大数量</br>
//		 */
//		public function getPointAround(p:Point,radius:uint,max:uint=0):Array
//		{
//			if(!this._currScene)
//				return new Array();
//				
//			var cx:uint=p.x/Common.tileWidth;
//			var cy:uint=p.y/Common.tileHeight;
//			
//			var x1:uint=cx-radius;
//			var x2:uint=cx+radius;
//			
//			var y1:uint=cy-radius;
//			var y2:uint=cy+radius;
//			
//			var re:Array=this._currScene.tree.getArea(y1,y2,x1,x2);
//			
//			if(re.length>=max && max!=0)
//				re.length=max;
//			
//			return re;		
//		}
		
//		/**
//		 * 寻找周围8个节点
//		 */
//		public function connectedNodeFunction(node:Leaf):Array
//		{
//			var array:Array=this.currScene.tree.getAroundLeaf(node.x,node.y,1);
//			return array;
//		}
		
//		/**
//		 * 
//		 * 深度排序
//		 */
//		public function depthSorting():void
//		{
//			if(!_currScene)
//				return;
////			var a:Array=
////			_objects.sortOn("y");
////			for each(var o:* in _objects)
////				_currScene.map.characterLayout.addChild(o.graphic);
//		}
			
		/**
		 * 将地图添加到场景
		 */
		public function addToStage():void
		{
//			_stage.removeChild(_loadingView);
			_miniMap=null;
			_miniMap=new MiniMap();
			_miniMap.init(_currScene.mapData,_currScene.camera,_currScene.smallBmp);
			_miniMap.x=(this._currScene.camera.width-_currScene.smallBmp.width);
			_miniMap.y=0;
//			_currScene.map.printBuff();
			UIManager.inst.uiLayer.addChild(_miniMap);
		}
		
		//********************* 内部使用 *********************
		protected function onSceneDataLoaded():void
		{
//			_currScene=_loadingScene;
//			_loadingView.info.text="地图加载完成等待进入场景！";
//			_loadingScene=null;		
			_isLoading=true;		
			this.dispatchEvent(new SceneEvent(SceneEvent.SCENE_LOADED,_currScene));
		}
		
		
		//********************* 属性 *********************
		
		/**
		 * 当前场景
		 */
		public function get currScene():Scene
		{
			return _currScene;
		}

		/**
		 * 场景和地图的舞台
		 */
		public function get stage():DisplayObjectContainer
		{
			return _stage;
		}

		/**
		 * @private
		 */
		public function set stage(value:DisplayObjectContainer):void
		{
			_stage = value;
		}

		public function get miniMap():MiniMap
		{
			return _miniMap;
		}

		public function set miniMap(value:MiniMap):void
		{
			_miniMap = value;
		}
		//************************私有*********************
		/**
		 * 地图点击事件
		 */
		protected function onClick(e:MouseEvent):void
		{
//			var local:Point=this.globalToLocal(new Point(e.stageX,e.stageY));
			var obj:Array=new Array();
			var ani:AnimSprite;
			var color:uint;
			var arr:Array=[];
			var local:Point=new Point(e.localX,e.localY);
			var global1:Point=new Point(e.stageX,e.stageY);
			var itemLocal:Point;
			
			arr=this.currScene.getObjectsUnderPoint(global1);
			obj.push(this.currScene.globalToLocal(global1));
			for each(var i:* in arr)
			{
				if(i is AnimSprite)
				{
					itemLocal=i.globalToLocal(global1);
					color=i.bitmapData.getPixel(itemLocal.x,itemLocal.y);
					if(color>0 && i.parent is Graphic)
						obj.push(i.parent.actor);
				}
			}
			

			Controller.instance.mapClick(obj);
		}

		//********************* 成员 *********************
		protected var _stage:DisplayObjectContainer;
		protected var _currScene:Scene;
		protected var _loadingView:LoadingView;
		protected var _miniMap:MiniMap;
		/**
		 * 正在加载的场景
		 */
//		protected var _loadingScene:Scene;//
		protected var _objects:Array;//场景中的游戏对像
		protected var _isLoading:Boolean;
		
	}
}